/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Components;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author Memet
 */
public class CheckBoxList extends JList{

    public CheckBoxList() {
	super(new DefaultListModel());
        this.setCellRenderer(new CheckListRenderer());
        this.addMouseListener(new MouseAdapter()
      {
            @Override
         public void mouseClicked(MouseEvent event)
         {
            JList list = (JList) event.getSource();

            // Get index of item clicked

            int index = list.locationToIndex(event.getPoint());
            CheckObject item = (CheckObject)
               list.getModel().getElementAt(index);

            // Toggle selected state

            item.setChecked(! item.isChecked());

            // Repaint cell

            list.repaint(list.getCellBounds(index, index));
         }
      });
    }
    public DefaultListModel getContents() {
	return (DefaultListModel)getModel();
    }

    public void selectAll()
    {
        for (int i = 0; i < getModel().getSize(); i++) {
            CheckObject item = (CheckObject) getModel().getElementAt(i);

            // Toggle selected state

            item.setChecked(true);
        }

        // Repaint cell

        repaint();
    }

    public void deselectAll()
    {
        for (int i = 0; i < getModel().getSize(); i++) {
            CheckObject item = (CheckObject) getModel().getElementAt(i);

            // Toggle selected state

            item.setChecked(false);
        }

        // Repaint cell

        repaint();
    }

    public Iterator<Object> selectedValues()
    {
        ArrayList<Object> lst = new ArrayList<Object>();
        for (int i = 0; i < getModel().getSize(); i++) {
            CheckObject item = (CheckObject) getModel().getElementAt(i);

            if(item.isChecked())
            {
                lst.add(item.getObject());
            }
        }
        return lst.iterator();
    }



}